import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.sendRedirect("login.html");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //检验提交的账号密码
        String userId = req.getParameter("userId");
        String passWord = req.getParameter("password");
        if (!userId.equals("zhang") || !passWord.equals("123456")){
            resp.getWriter().write("<div> 账号或密码错误 </div>");
            return;
        }
       if (req.getSession(false) == null){
           HttpSession httpSession = req.getSession(true);
           httpSession.setAttribute("userId",userId);
           httpSession.setAttribute("loginCount",0);
       }
        resp.sendRedirect("index");
    }
}
